<?php
namespace app\models;

use fastphp\base\Model;
use fastphp\db\Db;

class Problem extends Model
{
    protected $table = 'problem';
    public function search($keyword)
    {
        $sql = "select * from `$this->table` where `name` like :keyword";
        $sth = Db::pdo()->prepare($sql);
        $sth = $this->formatParam($sth, [':keyword' => "%$keyword%"]);
        $sth->execute();
        
        return $sth->fetchAll();
    }
    public function list($page, $limit, $desc=false, $category)
    {
        $problemList = (new Problem())->where(['category=?'], [$category])->order(['`id` ' . ($desc ? 'DESC' : 'ASC')])->limit([($page-1)*$limit . ',' .$limit])->fetchAll();
        return $problemList;
    }
    public function get($id)
    {
        return (new Problem())->where(['id=?'], [$id])->fetch();
    }
    public function analyse($problem)
    {
        if ($problem['author'][0] == '!')
        $problem['author_info'] = (new User())->getById(substr($problem['author'], 1));
        return $problem;
    }
}